package com.stormrage.edu.dao.adm.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.adm.AdmResDao;
import com.stormrage.entity.adm.AdmRes;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Repository
public class AdmResDaoImpl extends MysBaseDao implements AdmResDao {

	private static final Logger LOG = LoggerFactory.getLogger(AdmResDaoImpl.class);

	@Override
	public Integer queryCountByAcctId(final Map<String, Object> map) throws StormrageTechException {

		final String query = "SELECT COUNT(*) FROM adm_res AS a, adm_acct_res AS b"
				+ " WHERE a.id = b.res_id AND b.acct_id = :acctId";
		LOG.debug(" 数据库执行语句为： " + query);

		Integer count = queryCount(query, map);

		return count;
	}

	@Override
	public AdmRes queryById(final Map<String, Object> map) throws StormrageTechException {

		final String query = "SELECT * FROM adm_res WHERE id = :id";

		LOG.debug(" 数据库执行语句为： " + query);
		AdmRes entity = queryObject(query, AdmRes.class, map);

		return entity;
	}

	@Override
	public List<AdmRes> queryList() throws StormrageTechException {

		final String query = "SELECT * FROM adm_res";

		LOG.debug(" 数据库执行语句为： " + query);
		List<AdmRes> array = queryList(query, AdmRes.class);

		return array;
	}

	@Override
	public List<AdmRes> queryListByTypeParent(final Map<String, Object> map) throws StormrageTechException {

		final String query = "SELECT * FROM adm_res WHERE res_type = :resType AND parent_id = :parentId";

		LOG.debug(" 数据库执行语句为： " + query);
		List<AdmRes> array = queryList(query, AdmRes.class, map);

		return array;
	}

	@Override
	public List<AdmRes> queryListByAcctId(final Map<String, Object> map) throws StormrageTechException {

		final String query = "SELECT a.id, a.permission FROM adm_res AS a, adm_acct_res AS b"
				+ " WHERE a.id = b.res_id AND b.acct_id = :acctId";

		LOG.debug(" 数据库执行语句为： " + query);
		List<AdmRes> array = queryList(query, AdmRes.class, map);

		return array;
	}

}
